﻿using Sunny.UI;
using System.Collections.Generic;
using CoreShop.Model;
using CoreShop.Services;
using System;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using CoreShop.Model.UI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Security.Policy;
using SqlSugar.Extensions;
using System.Collections;
using CoreShop.Library;

namespace CoreShop.App
{
    public partial class FrmUserCategory : UIForm
    {
        string postStr;
        string retStr;
        public bool returnValue;
        public delegate void CloseFormPage(int pageIndex);
        public event CloseFormPage SetCloseFormPage;//新订单统计，frmMainWin.btn_NewBill
        public int mPageIndex = 0;
        public FrmUserCategory(int _mPageIndex)
        {
            mPageIndex = _mPageIndex;
            InitializeComponent();
        }

        public FrmUserCategory()
        {
            InitializeComponent();
        }

        #region 加载信息列表
        private void LoadDataList(string str)
        {
            var count = 0;
            List<corecmsusercategory> lstSource = new corecmsusercategoryServices().QueryPageAsync(ref count, 1, 20, str);
            btnPg.TotalCount = count;
            this.dgvDataList.AutoGenerateColumns = false;
            this.dgvDataList.DataSource = lstSource;
        }
        #endregion

        private void Form_Load(object sender, System.EventArgs e)
        {
            LoadDataList("");
        }

        private void btnAddInfo_Click(object sender, System.EventArgs e)
        {
            FrmUserCategoryEdit frm = new FrmUserCategoryEdit(new corecmsusercategory());
            //frm.Render();
            frm.ShowDialog();
            LoadDataList(txtSearch.Text.Trim());
            frm.Dispose();
        }

        private void btnUpdate_Click(object sender, System.EventArgs e)
        {
            if (dgvDataList.SelectedRows.Count == 1)
            {
                long MemberId = CommFunc.ConvertToInt64(dgvDataList.SelectedRows[0].Cells["id"].Value);
                List<corecmsusercategory> Member = new corecmsusercategoryServices().QueryAsync(MemberId);
                FrmUserCategoryEdit frm = new FrmUserCategoryEdit(Member[0]);
                //frm.Render();
                frm.ShowDialog();
                LoadDataList(txtSearch.Text.Trim());
                frm.Dispose();
            }
        }

        private void ubtn_delete_Click(object sender, EventArgs e)
        {
            if (dgvDataList.SelectedRows.Count == 1)
            {
                string sName = dgvDataList.SelectedRows[0].Cells["name"].Value.ToString();
                if (!ShowAskDialog("确定要删除[" + sName + "]的记录吗？")) return;
                Int64 custId = Convert.ToInt64(dgvDataList.SelectedRows[0].Cells["id"].Value);
                string msg = null;
                //custId = new PosRemoteServices(AppConstVars.sUrl).DelUserCategory(custId, AppConstVars.sysUser.ccessToken, ref msg);
                //if (custId > 0)
                //{
                //    bool bResult = new corecmsusercategoryServices().DeleteByIdAsync(custId);
                //    if (!bResult)
                //    {
                //        UIMessageBox.Show("删除失败", "系统提示", UIStyle.Red, UIMessageBoxButtons.OK);
                //        return;
                //    }
                //    UIMessageBox.Show("删除成功", "系统提示", UIStyle.Green, UIMessageBoxButtons.OK);
                //}
                //else
                //{
                //    UIMessageBox.Show("删除失败" + msg, "系统提示", UIStyle.Red, UIMessageBoxButtons.OK);
                //    return;
                //}
            }
            LoadDataList(txtSearch.Text.Trim());
        }
                
        private void btnSerach_Click(object sender, EventArgs e)
        {
            LoadDataList(txtSearch.Text.Trim());
        }

        private void ubtn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvSupplierList_CellDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            btnUpdate_Click(null, null);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {

        }
    }
}